
package my.GUIBank;

import java.sql.*;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author jason hello hmd 222,333
 */
public class ATM {
    private Connection connection;
    private String name;
    private String password;
    private long balance;
    Frame frame;
    

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
    private long id;
 
    
    
    public ATM() {
        new Frame(this).setVisible(true);
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String url = "jdbc:mysql://127.0.0.1:3306/atm?user=root";
            connection = DriverManager.getConnection(url);
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | SQLException ex) {
            System.err.print("Unable to connect!");
            System.exit(0);
        }
        
        
    }
    
    public static void main(String[] args){
        try {
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { }
        
        
        
        new ATM();
    }
  
    public long getBalance() {
        return balance;
    }

    public void setBalance(long balance) {
        this.balance = balance;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    
    public void withdraw(long amount){
        balance -= amount;
        updateBalance();
    }
    
    public void deposit(long amount){
        balance += amount;
        updateBalance();
    }

    private void updateBalance() {
        String query = String.format("update atm.accounts set balance = '%d' where id = '%d'", this.balance, this.id);
        
        try {
            Statement statement = connection.createStatement();
            statement.executeUpdate(query);
        } catch (SQLException ex) {}
    }
    
    void register(String name, String password, long balance) {
        this.name = name;
        this.password = password.toString();
        this.balance = balance;
        
        String query = String.format("insert into atm.accounts (name,password,balance) values ('%s','%s','%d')", name, password, balance);
        String idQuery = "select last_insert_id()";
        
        try {
            Statement statement = connection.createStatement();
            statement.executeUpdate(query);            
            ResultSet rs = statement.executeQuery(idQuery);
            if (rs.next()) this.id = rs.getInt(1);
        } catch (SQLException ex) {}
    }

    boolean login(long id, String password) {
        String query = String.format("select * from atm.accounts where id='%d' and password='%s'", id, password);
        
        try {
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(query);
            
            if (rs.next()) {
                this.id = rs.getInt("id");
                this.name = rs.getString("name");
                this.password = rs.getString("password");
                this.balance = rs.getInt("balance");
                return true;
            } else {
                return false;
            }
            
        } catch (SQLException ex) { }
        return false;
    }
    
}

